package com.etc.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.etc.dao.SiteDao;
import com.etc.entity.Site;
import com.etc.util.DBUtil;

public class SiteDaoImpl implements SiteDao{

	@Override
	public List<Site> queryAllSite() {
		String sql="SELECT * FROM site";
		CachedRowSet set = DBUtil.execQuery(sql);
		List<Site> list = new ArrayList<Site>();
		Site site=null;
		try {
			while (set.next()) {
				int s_id = set.getInt(1);
				String s_name = set.getString(2);
				 site = new Site(s_id, s_name);
				 list.add(site);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean addSite(Site site) {
		String sql="INSERT INTO site VALUES(null,?)";
		boolean b = DBUtil.executeUpdate(sql, site.getS_name());
		return b;
	}

	@Override
	public boolean rmvSite(int id) {
	     String sql="DELETE FROM site WHERE s_id=?";
	     boolean b = DBUtil.executeUpdate(sql, id);
		return b;
	}

	@Override
	public Site querySite(String name) {
		String sql="SELECT * FROM site where s_name=?";
		CachedRowSet set = DBUtil.execQuery(sql,name);
		Site site=null;
		try {
			while (set.next()) {
				int s_id = set.getInt(1);
				String s_name = set.getString(2);
				 site = new Site(s_id, s_name);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return site;
	}

	@Override
	public boolean upSite(Site site) {
		String sql="UPDATE site SET s_name=? WHERE s_id=?";
		boolean b = DBUtil.executeUpdate(sql, site.getS_name(),site.getS_id());
		return b;
	}

}
